package com.ebring.shareEducation.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.ebring.shareEducation.bean.Menu;
import com.ebring.shareEducation.bean.MenuCode;
import com.ebring.shareEducation.bean.RoleMenu;
import com.ebring.shareEducation.bean.admin.MenuAll;
import com.ebring.shareEducation.bean.response.IntToChart;
import com.ebring.shareEducation.bean.response.MenuOperation;
import com.ebring.shareEducation.dao.MenuCodeMapper;
import com.ebring.shareEducation.dao.MenuMapper;
import com.ebring.shareEducation.dao.MenuOperationCodeMapper;
import com.ebring.shareEducation.dao.MenuOperationMapperAll;
import com.ebring.shareEducation.dao.RoleMenuMapper;
import com.ebring.shareEducation.service.MenuService;
import com.ebring.shareEducation.utils.RspMsg;
import com.ebring.shareEducation.utils.UuidUtils;
import com.github.pagehelper.PageInfo;

@Service
@Transactional
public class MenuServiceImpl implements MenuService {

	@Autowired
	private MenuMapper menuMapper;

	@Autowired
	private RoleMenuMapper roleMenuMapper;

	@Autowired
	private MenuOperationCodeMapper menuOperationCodeMapper;

	@Autowired
	private MenuOperationMapperAll menuOperationMapperAll;

	@Autowired
	private MenuCodeMapper menuCodeMapper;

	// 查询所有的菜单
	@Override
	public List<Menu> selectMenu() {
		return menuMapper.selectAll();
	}

	// 修改菜单
	@Override
	public RspMsg<Menu> updateMenu(Menu menu) {
		RspMsg rspMsg = new RspMsg();
		menu.setMenuId(null);
		menu.setParentId(null);
		// menu.setOperation(null);
		int i = menuMapper.updateByPrimaryKeySelective(menu);
		if (i == 0) {
			rspMsg.setError();
			rspMsg.setMsg("修改失败");
			return rspMsg;
		}
		rspMsg.setOK();
		return rspMsg;

	}

	// 根据角色ID查找菜单
	@Override
	public RspMsg<RoleMenu> selectMenuByMenuId(String menuId,String roleId) {
		RspMsg rspMsg = new RspMsg<>();
		Menu list = menuMapper.selectBymenuId(menuId);
		List rolemenulist = roleMenuMapper.selectRoleMenuByMenuId(menuId,roleId);
		list.setRoleMenuList(rolemenulist);
		if (StringUtils.isEmpty(list)) {
			rspMsg.setError();
			rspMsg.setMsg("没有找到相关上级");
			return rspMsg;
		}
		rspMsg.setOK();
		rspMsg.setResult(list);
		return rspMsg;
	}
	// 根据角色ID查找菜单
/*
	@Override
	public RspMsg<Menu> selectMenuByMenuId(String menuId) {
		RspMsg rspMsg = new RspMsg<>();
		Menu list = menuMapper.selectBymenuId(menuId);

		if (StringUtils.isEmpty(list)) {
			rspMsg.setError();
			rspMsg.setMsg("没有找到相关上级");
			return rspMsg;
		}
		rspMsg.setOK();
		rspMsg.setResult(list);
		return rspMsg;
	}
*/

	// 根据ID删除菜单
	@Override
	public RspMsg<Menu> deleteById(String menuId) {
		RspMsg rspMsg = new RspMsg();
		int i = menuMapper.deleteByPrimaryKey(menuId);
		if (i == 0) {
			rspMsg.setError();
			rspMsg.setMsg("删除失败");
			return rspMsg;
		}

		rspMsg.setOK();
		return rspMsg;
	}

	// 新增菜单
	@Override
	@Transactional
	public RspMsg<Menu> insertMenu(Menu menu) {
		RspMsg rspMsg = new RspMsg();
		menu.setId(UuidUtils.getNumberId(6));
		String parentId = menu.getParentId();
		if (parentId.isEmpty() || parentId == null) {
			menu.setParentId("0");
			menu.setMenuGrade("1");
		} else {
			// 根据parentId查询父级级别
			Integer integer = menuMapper.selectparentId(parentId);
			if (integer != null) {
				Integer Grade = integer + 1;
				String menuGrade1 = Grade.toString();
				//查出parentId的总数
				int y = 2;
				String substring = parentId.substring(0, y);
				int parseInt = Integer.parseInt(substring);
				Integer inte = parseInt + 10;
				String str = inte.toString();
				System.err.println(str);
				Integer count = menuMapper.selectGradeIdCount(menuGrade1);
				if (count >= 9) {
					Integer countis = count + 1;
					String string = countis.toString();
					String concat = str.concat(string);
					menu.setParentId(parentId);
					menu.setMenuId(concat);
				} else {
					Integer countis = count + 1;
					String id = str + "0" + countis;
					System.err.println(id);
					menu.setParentId(parentId);
					menu.setMenuId(id);
				}
				// 查询最低级别
				// 级别+1 > 最大级别
				// 转换成汉字 拼接+级菜单
				// 添加到级别库

				String menuGrade = null;
				Integer GradeId = menuCodeMapper.selectMaxByGrade();
				if (Grade > GradeId) {
					menuGrade = Grade.toString();
					MenuCode menuCode = new MenuCode();
					IntToChart intToChart = new IntToChart();
					String string = intToChart.toChinese(menuGrade);
					menuCode.setId(UuidUtils.getNumberId(6));
					menuCode.setGradeName(string + "级菜单");
					menuCode.setMenuGrade(Grade.toString());
					int insertSelective = menuCodeMapper.insertSelective(menuCode);
				}
				menuGrade = Grade.toString();
				menu.setMenuGrade(menuGrade);
			} else {
				Integer count = menuMapper.selectparentIdCount(parentId);
				if (count >= 9) {
					Integer countis = count + 1;
					String string = countis.toString();
					String concat = "10".concat(string);
					menu.setParentId(parentId);
					menu.setMenuId(concat);
				} else {
					Integer countis = count + 1;
					String id = "10" + "0" + countis;
					System.err.println(id);
					menu.setParentId(parentId);
					menu.setMenuId(id);
				}
				menu.setMenuGrade("1");
			}
		}
		// menu.setOperation("add_del_edit_find");
		int i = menuMapper.insertSelective(menu);
		if (i == 0) {
			rspMsg.setError();
			rspMsg.setMsg("新增菜单失败");
			return rspMsg;
		}
		rspMsg.setOK();
		return rspMsg;
	}

	// 查询所有界面控件
	@Override
	public RspMsg<MenuOperation> selectOperation() {
		RspMsg rspMsg = new RspMsg();
		List operation = menuOperationCodeMapper.selectAll();
		if (operation.isEmpty() || operation == null) {
			rspMsg.setOK();
			rspMsg.setMsg("未找到相关数据");
			return rspMsg;
		}
		rspMsg.setOK();
		rspMsg.setResult(operation);
		return rspMsg;
	}

	// 根据菜单id查询界面控件
	public RspMsg<Menu> selectOperationById(String menuId) {
		RspMsg rspMsg = new RspMsg();
		List operation = menuMapper.selectOperationById(menuId);
		if (operation.isEmpty() || operation == null) {
			rspMsg.setOK();
			rspMsg.setMsg("未找到相关数据");
			return rspMsg;
		}
		rspMsg.setOK();
		rspMsg.setResult(operation);
		return rspMsg;
	}

	/**
	 * 查询当前id的所有父级
	 */
	@Override
	public RspMsg<List<MenuAll>> getParentAll(String menuId) {
		RspMsg rspMsg = new RspMsg();
		List list = new ArrayList();
		if (menuId != null || !menuId.isEmpty()) {
			int parseInt = Integer.parseInt(menuId);
			if (parseInt != 0) {
				MenuAll institution = menuOperationMapperAll.selectByPrimarymenuId(menuId);
				list.add(institution);
				String parentId = institution.getParentId();
				int i = Integer.parseInt(parentId);
				System.err.println(i);
				if (i == 0) {
					rspMsg.setOK();
					rspMsg.setResult(list);
					return rspMsg;
				} else {
					/*
					 * MenuAll parent = menuOperationMapperAll.selectgetParent(parentId); if
					 * (StringUtils.isEmpty(parent)) { rspMsg.setError(); rspMsg.setMsg("没有找到相关上级");
					 * return rspMsg; } list.add(parent); String id = parent.getParentId();
					 */
					int a = 0;
					while (a == 0) {
						MenuAll parent = menuOperationMapperAll.selectgetParent(parentId);
						if (parent != null) {
							list.add(parent);
						} else {
							break;
						}
						String parenId = parent.getParentId();
						parent = menuOperationMapperAll.selectgetParent(parenId);
						if (parent != null) {
							parentId = parent.getParentId();
							if (parent != null) {
								list.add(parent);
							} else {
								a = 1;
							}
							a = 0;
						} else {
							a = 1;
						}
					}
					rspMsg.setOK();
					rspMsg.setResult(list);
					return rspMsg;
				}
			}
			rspMsg.setOK();
			rspMsg.setMsg("没有父级");
			return rspMsg;
		}
		return rspMsg;
	}

	// 查询用户的菜单权限
	@Override
	public List<Menu> selectUMenu(String roleId) {
		RspMsg rspMsg = new RspMsg();
		List<Menu> list =null;
		if(roleId !=null){
			list = menuMapper.selectUMenu(roleId);
		}
		return list;
	}
	//给相对应的角色分配菜单
	/*@Override
	public List<HashMap> selectMenuByRoleId(String roleId) {
		//查询所有的菜单
		List menulist = menuMapper.selectAll();
		//通过角色id查询角色下面拥有的菜单
		List rolemenulist = roleMenuMapper.selectRoleMenu(roleId);
		ArrayList list = new ArrayList();
		for (int i = 0; i < menulist.size(); i++) {
			HashMap hp = new HashMap();
			Menu menu = (Menu) menulist.get(i);
			String menuId = menu.getMenuId();
			hp.put("cheks", new Integer(2));
			hp.put("menuname", menu.getMenuName());
			hp.put("menuid", menu.getMenuId());
			hp.put("upmenuid", menu.getParentId());
			hp.put("operation", menu.getOperation());
			hp.put("operflag","");
			for (int j = 0; j < rolemenulist.size(); j++) {
				RoleMenu roleMenu = (RoleMenu) rolemenulist.get(j);
				if (menuId.trim().equals(roleMenu.getMenuId().trim())) {
					hp.put("cheks", new Integer(1));
					String menustr = menu.getOperation();
					String rolestr = roleMenu.getOperation();
					if (menustr != null && rolestr != null) {
						String[] menus = menustr.split("_");
						String[] roles = rolestr.split("_");
						String flag = "";
						for (int n = 0; n < menus.length; n++) {
							if (menus[n] != null && !menus[n].equals("")) {
								String fla = "2_";
								for (int m = 0; m < roles.length; m++) {
									if (roles[m] != null && !roles[m].equals("")) {
										if (menus[n].trim().equals(roles[m].trim())) {
											fla = "1_";
										}
									} else {
									}
								}
								flag += fla;

							} else {
								flag = "";
							}
						}
						hp.put("operflag", flag);
					}
				}
			}
			list.add(hp);
		}
		return list;
	}*/

	/*public List<HashMap> selectMenuByRoleId(String roleId) {
		List menulist = menuMapper.selectAll();
		//通过角色id查询角色下面拥有的菜单
		List rolemenulist = roleMenuMapper.selectRoleMenu(roleId);
		ArrayList list = new ArrayList();
		for (int i = 0; i < menulist.size(); i++) {
			HashMap hp = new HashMap();
			Menu menu = (Menu) menulist.get(i);
			String menuId = menu.getMenuId();
			hp.put("cheks", new Integer(2));
			hp.put("menuname", menu.getMenuName());
			hp.put("menuid", menu.getMenuId());
			hp.put("upmenuid", menu.getParentId());
			hp.put("operation", menu.getOperation());
			hp.put("operflag", menu.getOperation());
			for (int j = 0; j < rolemenulist.size(); j++) {
				RoleMenu roleMenu = (RoleMenu) rolemenulist.get(j);
				if (menuId.trim().equals(roleMenu.getMenuId().trim())) {
					hp.put("cheks", new Integer(1));
					String menustr = menu.getOperation();
					String rolestr = roleMenu.getOperation();
					if (menustr != null && rolestr != null) {
						String[] menus = menustr.split("_");
						String[] roles = rolestr.split("_");
						String flag = "";
						for (int n = 0; n < menus.length; n++) {
							if (menus[n] != null && !menus[n].equals("")) {
								String fla = "2_";
								for (int m = 0; m < roles.length; m++) {
									if (roles[m] != null && !roles[m].equals("")) {
										if (menus[n].trim().equals(roles[m].trim())) {
											fla = "1_";
										}
									} else {
									}
								}
								flag += fla;

							} else {
								flag = "";
							}
						}
						hp.put("operflag", flag);
					}
				}
			}
			list.add(hp);
		}
		return list;
	}
*/
	//给相对应的角色分配菜单

	public List<HashMap> selectMenuByRoleId(String roleId) {
		List menulist = menuMapper.selectAll();
		List rolemenulist = roleMenuMapper.selectRoleMenu(roleId);
		ArrayList list = new ArrayList();
		for (int i = 0; i < menulist.size(); i++) {
			HashMap hp = new HashMap();
			Menu menu = (Menu) menulist.get(i);
			String menuId = menu.getMenuId();
			hp.put("cheks", new Integer(2));
			hp.put("menuname", menu.getMenuName());
			hp.put("menuid", menu.getMenuId());
			hp.put("upmenuid", menu.getParentId());
			hp.put("operation", menu.getOperation());
			hp.put("operflag", menu.getOperation());
			for (int j = 0; j < rolemenulist.size(); j++) {
				RoleMenu roleMenu = (RoleMenu) rolemenulist.get(j);
				if (menuId.trim().equals(roleMenu.getMenuId().trim())) {
					hp.put("cheks", new Integer(1));
					String menustr = menu.getOperation();
					String rolestr = roleMenu.getOperation();
					if (menustr != null && rolestr != null) {
						String[] menus = menustr.split("_");
						String[] roles = rolestr.split("_");
						String flag = "";
						for (int n = 0; n < menus.length; n++) {
							if (menus[n] != null && !menus[n].equals("")) {
								String fla = "2_";
								for (int m = 0; m < roles.length; m++) {
									if (roles[m] != null && !roles[m].equals("")) {
										if (menus[n].trim().equals(roles[m].trim())) {
											fla = "1_";
										}
									} else {
									}
								}
								flag += fla;

							} else {
								flag = "";
							}
						}
						hp.put("operflag", flag);
					}
				}
			}
			list.add(hp);
		}
		return list;
	}
	@Override
	public List<HashMap> updateMenus(String menus,String roleId) {
		int num = 0;
		//通过角色id删除角色下面的菜单
		num = roleMenuMapper.deleteRoleMenuByRoleId(roleId);
		System.out.println("删除" + num + "条数据");
		String[] array = menus.split("|");
		for (int i = 0; i < array.length; i++) {
			if (array[i] != null && !(array[i].trim().equals(""))) {
				RoleMenu roleMenu = new RoleMenu();
				roleMenu.setId(UuidUtils.getNumberId(10));
				System.out.println(array[i].indexOf("_"));
				roleMenu.setMenuId(array[i].substring(0, array[i].indexOf("_")));
				roleMenu.setRoleId(roleId);
				roleMenu.setOperation(array[i].substring(array[i].indexOf("_") + 1).trim());
				roleMenu.setRemark("");
				roleMenuMapper.insert(roleMenu);
				/*RoleMenu list=roleMenuMapper.selectByPrimaryKey(roleMenu.getId());
				Menu menu=menuMapper.selectBymenuId(list.getMenuId());
				menu.setOperation(array[i].substring(array[i].indexOf("_") + 1).trim());*/
				/*int j=menuMapper.updateByPrimaryKey(menu);*/
			}
		}
		List list = new ArrayList();
		Map map = new HashMap();
		map.put("code", "1");
		list.add(map);
		return list;
	}

	@Override
	public RspMsg<List<MenuAll>> selectMenuGrade() {
		RspMsg rspMsg = new RspMsg();
		Map<String, Object> map = new LinkedHashMap<>();
		List<MenuAll> list = menuOperationMapperAll.selectMenu();
		if (list.isEmpty() || list == null) {
			rspMsg.setOK();
			rspMsg.setMsg("未找到相关数据");
			return rspMsg;
		}
		PageInfo<MenuAll> pageInfo = new PageInfo<>(list);
		// 总条数
		map.put("total", pageInfo.getTotal());
		// 每页显示条数
		map.put("curPage", pageInfo.getPageSize());
		// 总页数
		map.put("pages", pageInfo.getPages());
		// 当前页
		map.put("pageNum", pageInfo.getPageNum());
		map.put("menuList", list);
		rspMsg.setOK();
		rspMsg.setResult(map);
		return rspMsg;
	}

	@Override
	public RspMsg<MenuAll> getLower(String menuId) {
		RspMsg rspMsg = new RspMsg();
		Map<String, Object> map = new LinkedHashMap<>();
		List<MenuAll> menuIdList = menuOperationMapperAll.selectGetLower(menuId);
		if (menuIdList.isEmpty() || menuIdList == null) {
			rspMsg.setError();
			rspMsg.setMsg("没有找到相关下级");
			return rspMsg;
		}
		PageInfo<MenuAll> pageInfo = new PageInfo<>(menuIdList);
		// 总条数
		map.put("total", pageInfo.getTotal());
		// 每页显示条数
		map.put("curPage", pageInfo.getPageSize());
		// 总页数
		map.put("pages", pageInfo.getPages());
		// 当前页
		map.put("pageNum", pageInfo.getPageNum());
		map.put("menuList", menuIdList);
		rspMsg.setOK();
		rspMsg.setResult(map);
		return rspMsg;
	}

	@Override
	public RspMsg<MenuAll> getParent(String menuId) {
		RspMsg rspMsg = new RspMsg();
		MenuAll institution = menuOperationMapperAll.selectByPrimarymenuId(menuId);
		String parentId = institution.getParentId();
		System.err.println(parentId);
		if (parentId == null) {
			return null;
		}
		MenuAll parent = menuOperationMapperAll.selectgetParent(parentId);
		if (StringUtils.isEmpty(parent)) {
			rspMsg.setError();
			rspMsg.setMsg("没有找到相关上级");
			return rspMsg;
		}

		rspMsg.setOK();
		rspMsg.setResult(parent);
		return rspMsg;
	}

	@Override
	public RspMsg<List<MenuAll>> select(String menuId, String menuName,String menuGrade,String parentId) {
		RspMsg rspMsg = new RspMsg();
		Map<String, Object> map = new LinkedHashMap<>();
/*		String str1 = null;
		String str2 = null;
		if (null != menuId) {
			str1 = "%" + menuId + "%";
		}
		if (null != menuName) {
			str2 = "%" + menuName + "%";
		}*/
		List<MenuAll> menuList = menuOperationMapperAll.selectLike(menuId, menuName,menuGrade,parentId);
		if (menuList.isEmpty() || menuList == null) {
			rspMsg.setOK();
			rspMsg.setMsg("没有找到相关机构");
			return rspMsg;
		}
		PageInfo<MenuAll> pageInfo = new PageInfo<>(menuList);
		// 总条数
		map.put("total", pageInfo.getTotal());
		// 每页显示条数
		map.put("curPage", pageInfo.getPageSize());
		// 总页数
		map.put("pages", pageInfo.getPages());
		// 当前页
		map.put("pageNum", pageInfo.getPageNum());
		map.put("menuList", menuList);
		rspMsg.setOK();
		rspMsg.setResult(map);
		return rspMsg;
	}
}
